//
//  TTScreenItemCollectionViewCell.m
//  TaoTuo
//
//  Created by LG on 2020/11/17.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "LLScreenItemCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@interface LLScreenItemCollectionViewCell ()

/// 标题
@property (nonatomic, strong) UIButton *btn;
/// 图片
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation LLScreenItemCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColorHex(#F5F5F5);
        
        self.layer.cornerRadius = 5;
        self.layer.borderWidth = 1;
        self.layer.borderColor = self.backgroundColor.CGColor;
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn setTitleColor:UIColorHex(333333) forState:UIControlStateDisabled];
        _btn.titleLabel.font = [UIFont systemFontOfSize:13];
        _btn.frame = self.bounds;
        _btn.enabled = NO;
        [self addSubview:_btn];
        
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _imageView.layer.cornerRadius = 5;
        _imageView.layer.masksToBounds = YES;
        [self addSubview:_imageView];
        
        
    }
    return self;
}


- (void)setModel:(LLScreenItemModel *)model
{
    _model = model;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:model.title];
    if (!model.isEnable) {
        [attr addAttributes:@{NSStrikethroughStyleAttributeName : @1, NSForegroundColorAttributeName : UIColorHex(999999)} range:[model.title rangeOfAll]];
    } else {
        [attr addAttributes:@{NSStrikethroughStyleAttributeName : @0, NSForegroundColorAttributeName : UIColorHex(333333)} range:[model.title rangeOfAll]];
    }
    [self.btn setAttributedTitle:attr forState:UIControlStateDisabled];
    
    if (model.imageName) {
        if ([model.imageName hasPrefix:@"http"]) {
            NSString *urlStr = [model.imageName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageWithColor:UIColorHex(#F5F5F5)] options:SDWebImageRetryFailed];
        } else {
            self.imageView.image = [UIImage imageNamed:model.imageName];
        }
    }
    
    self.imageView.hidden = model.imageName.length == 0;
    self.btn.hidden = !self.imageView.hidden;
    
    [self setSelected:model.isSelected];
    
    _btn.frame = self.bounds;
    _imageView.frame = self.bounds;
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    
    if (self.model.btnImageName.length > 0) {
        NSString *imageName = selected ? [NSString stringWithFormat:@"%@_sel", self.model.btnImageName] : self.model.btnImageName;
        [self.btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateDisabled];
    } else {
        [self.btn setImage:[UIImage imageNamed:@""] forState:UIControlStateDisabled];
    }
    
    
    if (!self.model.isEnable) {
        return;
    }
    
    NSMutableAttributedString *attr = self.btn.currentAttributedTitle.mutableCopy;
    if (selected) {
        self.layer.borderColor = UIColorHex(#8B57E6).CGColor;
        self.backgroundColor = UIColorHex(#F5F0FF);
        [attr addAttributes:@{NSForegroundColorAttributeName : UIColorHex(8B57E6)} range:[self.model.title rangeOfAll]];
    } else {
        self.backgroundColor = UIColorHex(#F5F5F5);
        self.layer.borderColor = self.backgroundColor.CGColor;
        [attr addAttributes:@{NSForegroundColorAttributeName : UIColorHex(333333)} range:[self.model.title rangeOfAll]];
    }
    
    [self.btn setAttributedTitle:attr forState:UIControlStateDisabled];
}


@end
